<%#
 Copyright 2013-2018 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
    <%_
    const instanceType = (dto === 'mapstruct') ? entityClass + 'DTO' : entityClass;
    const instanceName = (dto === 'mapstruct') ? entityInstance + 'DTO' : entityInstance;
    const mapper = entityInstance + 'Mapper';
    const entityListToDtoListReference = mapper + '.' + 'toDto';
    const entityToDtoReference = mapper + '::'+ 'toDto';
    if (jpaMetamodelFiltering) {  %>
    public ResponseEntity<List<<%= instanceType %>>> getAll<%= entityClassPlural %>(<%= entityClass %>Criteria criteria<% if (pagination != 'no') { %>, Pageable pageable<% } %>) {
        log.debug("REST request to get <%= entityClassPlural %> by criteria: {}", criteria);
    <%_ if (pagination === 'no') { _%>
        List<<%= instanceType %>> entityList = <%= entityInstance %>QueryService.findByCriteria(criteria);
        return ResponseEntity.ok().body(entityList);
    <%_ } else { _%>
        Page<<%= instanceType %>> page = <%= entityInstance %>QueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/<%= entityApiUrl %>");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    <%_ } _%>
    }

    /**
    * GET  /<%= entityApiUrl %>/count : count all the <%= entityInstancePlural %>.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/<%= entityApiUrl %>/count")
    @Timed
    public ResponseEntity<Long> count<%= entityClassPlural %> (<%= entityClass %>Criteria criteria) {
        log.debug("REST request to count <%= entityClassPlural %> by criteria: {}", criteria);
        return ResponseEntity.ok().body(<%= entityInstance %>QueryService.countByCriteria(criteria));
    }
<% } else { %>
    <%_ if (pagination === 'no') { _%>
    public List<<%= instanceType %>> getAll<%= entityClassPlural %>(<% if (fieldsContainNoOwnerOneToOne) { %>@RequestParam(required = false) String filter<% } %><% if (fieldsContainOwnerManyToMany && fieldsContainNoOwnerOneToOne) { %>,<% } %><% if (fieldsContainOwnerManyToMany) { %>@RequestParam(required = false, defaultValue = "false") boolean eagerload<% }%>) {<%- include('get_all_stream_template', {viaService: viaService}); -%>
        log.debug("REST request to get all <%= entityClassPlural %>");<% if (viaService) { %>
        return <%= entityInstance %>Service.findAll();<% } else if (dto === 'mapstruct') { %>
        List<<%= entityClass %>> <%= entityInstancePlural %> = <%= entityInstance %>Repository.<% if (fieldsContainOwnerManyToMany) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>();
        return <%= entityListToDtoListReference %>(<%= entityInstancePlural %>);<% } else { %>
        return <%= entityInstance %>Repository.<% if (fieldsContainOwnerManyToMany) { %>findAllWithEagerRelationships<% } else { %>findAll<% } %>();<% } %>
    <%_ } if (pagination !== 'no') { _%>
    public ResponseEntity<List<<%= instanceType %>>> getAll<%= entityClassPlural %>(Pageable pageable<% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) String filter<% } %><% if (fieldsContainOwnerManyToMany) { %>, @RequestParam(required = false, defaultValue = "false") boolean eagerload<% } %>) {<%- include('get_all_stream_template', {viaService: viaService}); -%>
        log.debug("REST request to get a page of <%= entityClassPlural %>");
    <%_ if (viaService) { _%>
        <%_ if (fieldsContainOwnerManyToMany) { _%>
        Page<<%= instanceType %>> page;
        if (eagerload) {
            page = <%= entityInstance %>Service.findAllWithEagerRelationships(pageable);
        } else {
            page = <%= entityInstance %>Service.findAll(pageable);
        }
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, String.format("/api/<%= entityApiUrl %>?eagerload=%b", eagerload));
        <%_ } else { _%>
        Page<<%= instanceType %>> page = <%= entityInstance %>Service.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/<%= entityApiUrl %>");
        <%_ } _%>
    <%_ } else { _%>
        <%_ if (fieldsContainOwnerManyToMany) { _%>
        Page<<%= instanceType %>> page;
        if (eagerload) {
            page = <%= entityInstance %>Repository.findAllWithEagerRelationships(pageable)<% if (dto === 'mapstruct') {%>.map(<%= entityToDtoReference %>)<% } %>;
        } else {
            page = <%= entityInstance %>Repository.findAll(pageable)<% if (dto !== 'mapstruct') { %>;<% } else { %>.map(<%= entityToDtoReference %>);<% } %>
        }
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, String.format("/api/<%= entityApiUrl %>?eagerload=%b", eagerload));
        <%_ } else { _%>
        Page<<%= instanceType %>> page = <%= entityInstance %>Repository.findAll(pageable)<% if (dto !== 'mapstruct') { %>;<% } else { %>.map(<%= entityToDtoReference %>);<% } %>
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/<%= entityApiUrl %>");
        <%_ } _%>
    <%_ } _%>
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    <%_ } _%>
    }
<%_ } _%>
